﻿using System;
using System.Collections.Generic;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Configuration;

namespace Directory.Web
{
	public partial class Search : System.Web.UI.Page
	{
		protected int pageIndex;
		protected int pageSize = WebSettings.GetInstance().SearchItemPageSize;
		protected int itemsRecords;
		protected string categoryIdKey = "categoryId";
		protected string pageKey = "page";
		protected string keywordsKey = "key";
		protected string searchtypeKey = "searchtype";

		protected void Page_Load(object sender, EventArgs e)
		{
			int searchtype = 0;
			int.TryParse(Request.QueryString[searchtypeKey], out searchtype);

			if (!IsPostBack)
			{
				ddlSearchType.Items.Clear();
				ddlSearchType.Items.Add(Resources.Resource.Item);
				ddlSearchType.Items.Add(Resources.Resource.Category);
				ddlSearchType.Items.Add("全部");
				ddlSearchType.SelectedIndex = 0;

				if (!int.TryParse(Request.QueryString[pageKey], out pageIndex))
				{
					pageIndex = 0;
				}
				string keywords = string.Empty;
				try
				{
					keywords = Server.UrlDecode(Request.QueryString[keywordsKey]).Trim();
				}
				catch
				{
					ltMsg.Text = "请输入查询关键字。";
					return;
				}
				if (string.IsNullOrEmpty(keywords))
				{
					ltMsg.Text = "请输入查询关键字。";
					return;
				}

				ddlSearchType.SelectedIndex = searchtype;

				try
				{
					if (searchtype == 1 || searchtype == 2)
					{
						rptCateogrys.DataSource = CategoryDataProxy.SearchCategories(keywords);
						rptCateogrys.DataBind();
						this.pagebreak.Visible = false;
					}
					if (searchtype == 0 || searchtype == 2)
					{
						rptItemsList.DataSource = ItemDataProxy.SearchItems(-999, keywords, pageIndex, pageSize, out itemsRecords);
						rptItemsList.DataBind();
						this.pagebreak.Visible = true;
						SetPageNav();
					}
				}
				catch
				{
					tbKeywords.Text = "抱歉，出错了。";
					return;
				}

				tbKeywords.Text = keywords;
			}
		}

		protected void btnSearch_Click(object sender, EventArgs e)
		{
			Server.Transfer("Search.aspx?" + keywordsKey + "=" + Server.UrlEncode(tbKeywords.Text) + "&" + searchtypeKey + "=" + ddlSearchType.SelectedIndex);
		}

		private string GetPageNavigateUrl(int _pageIndex)
		{
			return "Search.aspx?" + "&" + keywordsKey + "=" + Request.QueryString[keywordsKey] + "&" + pageKey + "=" + _pageIndex.ToString() + "&" + searchtypeKey + "=" + ddlSearchType.SelectedIndex;
		}

		private void SetPageNav()
		{
			int pageCounts = ((itemsRecords - 1) / pageSize) + 1;
			lbCurrentPage.Text = (pageIndex + 1) + " / " + pageCounts;

			if (pageCounts < 1)
			{
				hplFirstPage.Enabled = false;
				hplPrePage.Enabled = false;
				hplNextPage.Enabled = false;
				hplLastPage.Enabled = false;
			}
			else if (pageIndex == 0)
			{
				hplFirstPage.Enabled = false;
				hplPrePage.Enabled = false;
				if (pageCounts == 1)
				{
					hplNextPage.Enabled = false;
					hplLastPage.Enabled = false;
				}
				else
				{
					hplNextPage.Enabled = true;
					hplNextPage.NavigateUrl = GetPageNavigateUrl(pageIndex + 1);
					hplLastPage.Enabled = true;
					hplLastPage.NavigateUrl = GetPageNavigateUrl(pageCounts - 1);
				}
			}
			else if (pageIndex >= pageCounts - 1)
			{
				pageIndex = pageCounts - 1;
				hplFirstPage.Enabled = true;
				hplFirstPage.NavigateUrl = GetPageNavigateUrl(0);
				hplPrePage.Enabled = true;
				hplPrePage.NavigateUrl = GetPageNavigateUrl(pageIndex - 1);
				hplNextPage.Enabled = false;
				hplLastPage.Enabled = false;
			}
			else
			{
				hplFirstPage.Enabled = true;
				hplFirstPage.NavigateUrl = GetPageNavigateUrl(0);
				hplPrePage.Enabled = true;
				hplPrePage.NavigateUrl = GetPageNavigateUrl(pageIndex - 1);
				hplNextPage.Enabled = true;
				hplNextPage.NavigateUrl = GetPageNavigateUrl(pageIndex + 1);
				hplLastPage.Enabled = true;
				hplLastPage.NavigateUrl = GetPageNavigateUrl(pageCounts - 1);
			}
		}

		protected void rptItemsList_ItemDataBound(object sender, RepeaterItemEventArgs e)
		{
			Directory.Model.ItemInfo item = (Directory.Model.ItemInfo)e.Item.DataItem;
			Literal lt = e.Item.FindControl("ltCategoryPath") as Literal;
			if (lt != null && item != null)
			{
				foreach (Directory.Model.CategoryInfo cat in CategoryDataProxy.GetRecursiveParentsOfCategory(item.CategoryId))
				{
					string link = string.Format("<a href='#' onclick=\"window.open('categorydetails.aspx?categoryId={0}')\">{1}</a>", cat.CategoryId.ToString(), cat.CategoryName);
					lt.Text += "»" + link;
				}
			}
		}
	}
}
